/*登录、注册、找回密码三个页面*/
//参考网址  http://www.runoob.com/jquery/jquery-plugin-validate.html
$(document).ready(function () {
    // validate signup form on keyup and submit
    var icon = "*";
    //以下为登录页面
    $("#signupForm").validate({
        rules: {
            telphone:{
                required: true,
                number:true,
                minlength:11,
                maxlength: 11,
                isMobile : true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 15,
            }
        },
        messages: {
            telphone:{
                required: icon + "请输入手机号码",
                number:icon +"必须输入有效的数字",
                minlength:icon +"必须输入11位有效的数字",
                maxlength:icon +"必须输入11位有效的数字",
                isMobile:icon +"请输入正确的手机号"
            },
            password: {
                required: icon + "请输入您的密码",
                minlength: icon + "密码必须6-15个字符",
                maxlength: icon + "密码必须6-15个字符"
            }
        }
    });
    //以下为注册页面
    $("#regForm").validate({
        rules: {
            telphone:{
                required: true,
                number:true,
                minlength: 11,
                maxlength: 11,
                isMobile : true,
                remote: {
					url: "/bdyz/web/logIn/isTelExist",
					cache: false
				}
            },
            validateCode:{
            	required: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 15
            },
            confirm_password: {
                required: true,
                equalTo: "#password"
            },
            agree:"required"
        },
        messages: {
            telphone:{
                required: icon + "请输入手机号码",
                number:icon +"必须输入有效的数字",
                minlength:icon +"请输入11位有效数字",
                maxlength:icon +"请输入11位有效数字",
                isMobile:icon +"手机号格式错误",
                remote:icon +"该手机号已注册"
                
            },
            validateCode:{
            	required: icon + "请输入验证码"
            },
            password: {
                required: icon + "请输入您的密码",
                minlength: icon + "密码必须6-15个字符",
                maxlength: icon + "密码必须6-15个字符"
            },
            confirm_password: {
                required: icon + "请再次输入密码",
                equalTo: icon + "两次输入的密码不一致"
            },
            agree: {
                required: icon + "必须同意协议后才能注册",
                element: '#agree-error'
            }
        },
		submitHandler: function(form) {
			reg();
		}
    })
    // 手机号码验证
	jQuery.validator.addMethod("isMobile", function(value, element) {
		var length = value.length;
		var mobile = /^1[3|4|5|7|8][0-9]\d{4,8}$/;
		return this.optional(element) || (length == 11 && mobile.test(value));
	}, "请正确填写您的手机号码");
    //以下为忘记密码页面
    $("#ForgetForm").validate({
        rules: {
            telphone:{
            	required: true,
                number:true,
                minlength: 11,
                maxlength: 11,
                isMobile : true,
                remote: {
					url: "/bdyz/web/logIn/isTelRegister",
					cache: false
				}
            },
            validateCode:{
                required: true
            },
            password: {
            	required: true,
                minlength: 6,
                maxlength: 15
            },
            confirm_password: {
            	required: true,
                equalTo: "#password"
            }
        },
        messages: {
            telphone:{
            	required: icon + "请输入手机号码",
                number:icon +"必须输入有效的数字",
                minlength:icon +"请输入11位有效数字",
                maxlength:icon +"请输入11位有效数字",
                isMobile:icon +"手机号格式错误",
                remote:icon +"该手机号已注册"
            },
            validateCode:{
              required:icon+"请输入验证码"
            },
            password: {
            	required: icon + "请输入您的密码",
                minlength: icon + "密码必须6-15个字符",
                maxlength: icon + "密码必须6-15个字符"
            },
            confirm_password: {
            	required: icon + "请再次输入密码",
                equalTo: icon + "两次输入的密码不一致"
            }
        },
		submitHandler: function(form) {
			forgetPassword();
		}
    })
});
//注册
function reg(){
	$.ajax({
		url: '/bdyz/web/logIn/register',
		type:'POST',
		data:$("#regForm").serialize(),
		success:function(result){
			if(result =="注册成功"){
				alert(result);
				window.setTimeout("window.location.href='/bdyz/web/member/login'",2000);
			}else{
				alert(result);
			}
		}
	});
}
//忘记密码
function forgetPassword(){
	$.ajax({
		url: '/bdyz/web/logIn/forgotPassword',
		type:'POST',
		data:$("#ForgetForm").serialize(),
		success:function(result){
			if(result =="密码重置成功"){
				alert(result);
				window.setTimeout("window.location.href='/bdyz/web/member/login'",2000);
			}else{
				alert(result);
			}
		}
	});
}
//注册时发送验证码
var countdown = 60;
function getcode(obj) {
	var tel=$("#telphone").val();
	if(tel!=null&&tel!=""){
		var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
		 if (tel.length!=11) {
			alert("请输入11位手机号");
			$("#telphone").focus();
			return false; 
		 }
		 if(!reg.test(tel)){
			 alert("手机号格式错误！");
			$("#telphone").focus();
			return false;  
		 }
		$.ajax({
    		url: '/bdyz/web/logIn/isTelExist',
    		type:'POST',
    		data:{telphone:tel},
    		async : false,
    		success:function(result){
    			if(!result){
    				alert("该手机号已注册，请重新输入！");
    				$("#telphone").focus();
    				return false;
    			}else{
    				$.ajax({
    		    		url: '/bdyz/web/logIn/getcode',
    		    		type:'POST',
    		    		data:{telphone:tel},
    		    		success:function(result){
    		    			if(result.msg.status==1){
    		    				alert("发送成功！");
    		    			}else{
    		    				alert(result.msg.desc);
    		    			}
    		    		}
    		    	});
    				settime(obj);
    			}
    		}
    	});
	}else{
		alert("请输入手机号");
		$("#telphone").focus();
		return false;
	}
}
//忘记密码发送验证码
function getforcode(obj) {
	var tel=$("#telphone").val();
	if(tel!=null&&tel!=""){
		var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
		 if (tel.length!=11||!reg.test(tel)) {
			alert("手机号格式错误！");
			$("#telphone").focus();
			return false; 
		 }
		$.ajax({
    		url: '/bdyz/web/logIn/isTelRegister',
    		type:'POST',
    		data:{telphone:tel},
    		async : false,
    		success:function(result){
    			if(!result){
    				alert("该手机号尚未注册，请重新输入！");
    				$("#telphone").focus();
    				return false;
    			}else{
    				$.ajax({
    		    		url: '/bdyz/web/logIn/getcode',
    		    		type:'POST',
    		    		data:{telphone:tel},
    		    		success:function(result){
    		    			if(result.msg.status==1){
    		    				alert("发送成功！");
    		    			}else{
    		    				alert(result.msg.desc);
    		    			}
    		    		}
    		    	});
    				settime(obj);
    			}
    		}
    	});
	}else{
		alert("请输入手机号");
		$("#telphone").focus();
		return false;
	}
}
//倒计时
function settime(obj) {
	if (countdown == 0) {
        obj.removeAttribute("disabled");
        obj.value = "获取验证码";
        countdown = 60;
        return;
    } else {
        obj.setAttribute("disabled", true);
        obj.value = "重新发送(" + countdown + ")";
        countdown--;
    }
    setTimeout(function () {settime(obj)}, 1000)
}
//ie789placeholder不兼容
/*
$('#telphone').placeholder({
    word: '请输入您的手机号',
    evtType: 'keydown'
})
$('#password').placeholder({
    word: '登录密码',
    evtType: 'keydown'
})*/
